package de.ifi.lmu.snitzelhunt.participants;

import de.ifi.lmu.snitzelhunt.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

/**
 * 
 * @author Sebastian Siepe
 *
 */
public class PopupImageTask extends DialogFragment 
{
	private Long penaltyTime = 5L;
	private Bitmap image = null;
	private String title = "";
	private Context context;
	private String buttonText = "Nicht geschafft";
	

	public void setButton(String buttonText)
	{
		this.buttonText = buttonText;
	}
	
	
	
	
	public void setBitmap(Bitmap image, Context context)
	{
		this.image = image;
		this.context = context;
	}
	
	public void setTitle(String title)
	{
		this.title = title;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		// 1. Instantiate an AlertDialog.Builder with its constructor
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		//ImageView view =(ImageView) image;

		// 2. Chain together various setter methods to set the dialog
		// characteristics
		builder.setTitle(title);

		//builder.setView(view);

		builder.setIcon(R.drawable.image_task);

		
		builder.setCancelable(false);
		
		
		
		BitmapDrawable bitmap = new BitmapDrawable(context.getResources(), image);

		//builder imageDialog = new AlertDialog.Builder(context);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View layout = inflater.inflate(R.layout.thumbnail, null);
		ImageView imageView = (ImageView) layout.findViewById(R.id.thumbnail_IMAGEVIEW);
		imageView.setImageDrawable(bitmap);
		
		builder.setView(layout);

		// 3. Get the AlertDialog from create()
		// AlertDialog dialog = builder.create();

		builder.setNegativeButton(buttonText, new DialogInterface.OnClickListener() 
		{
			public void onClick(DialogInterface dialog, int id) 
			{
				Intent nextTask = new Intent("NEXT_TASK");
				nextTask.putExtra("PENALTY_TIME", penaltyTime);
				getActivity().sendBroadcast(nextTask);
				//!!!!!!!!!!!!
				dismissAllowingStateLoss();
			}
		});
		
		
		builder.setCancelable(false);
		
		// Create the AlertDialog object and return it
		return builder.create();
	}
	@Override
	public void show(FragmentManager manager, String tag) {
		// TODO Auto-generated method stub
		try {
			super.show(manager, tag);
		} catch (IllegalStateException ex) {
		}
	}
}